package org.act.xservices.rt.xcontainer.transport.http.codec;

import java.net.InetSocketAddress;

import org.apache.asyncweb.common.HttpRequest;
import org.apache.asyncweb.server.ServiceContainer;
import org.apache.asyncweb.server.context.AbstractHttpServiceContext;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.future.WriteFuture;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @LastModifyTime 
 * @CreationTime 2008-12-01
 * @author Lizq [lizq@act.buaa.edu.cn]
 * @since JDK1.5
 */

public class ATFHttpServiceContext extends AbstractHttpServiceContext {

	private static final Logger LOG = LoggerFactory.getLogger( ATFHttpServiceContext.class );
	
    /** the session bound to this ATFHttpServiceContext */
    private final IoSession session;
	
    private WriteFuture writeFuture;

    public ATFHttpServiceContext( IoSession session, 
    		                       HttpRequest request, ServiceContainer container )
    {
    	super( (InetSocketAddress)session.getLocalAddress(), 
        		(InetSocketAddress)session.getRemoteAddress(), request, container  );
        this.session = session;
    }

    public WriteFuture getWriteFuture()
    {
        return writeFuture;
    }

    public void setWriteFuture( WriteFuture writeFuture )
    {
        if ( ! isResponseCommitted() )
        {
            throw new IllegalStateException();
        }
        this.writeFuture = writeFuture;
    }

    @Override
    protected void doWrite( boolean requiresClosure )
    {
        WriteFuture future = session.write( this );
        if ( requiresClosure )
        {
            LOG.debug( "Added CLOSE future listener." );
            future.addListener( IoFutureListener.CLOSE );
        }
    }


    public void fireClientIdle( long idleTime, int idleCount )
    {
        super.fireClientIdle( idleTime, idleCount );
    }
    

    public void fireClientDisconnected()
    {
        super.fireClientDisconnected();
    }

	public IoSession getIoSession() {
		return session;
	}
}
	
	
